<template>
  <div class="app-container">
    <!-- 查询区域 -->
    <el-card class="box-card" shadow="hover">
      <div slot="header" class="clearfix">
        <span class="card-title">查询条件</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="showSearch = !showSearch">
          {{ showSearch ? '收起' : '展开' }}
        </el-button>
      </div>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="班级名称" prop="bjMc">
              <el-input v-model="queryParams.bjMc" placeholder="请输入班级名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" style="text-align: right">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 操作按钮区域 -->
    <el-card class="box-card" shadow="hover" style="margin-top: 15px">
      <div class="table-operations">
        <el-button-group>
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['jxgl:bjgl:add']">新增</el-button>
          <el-button type="success" icon="el-icon-edit" :disabled="single" @click="handleUpdate" v-hasPermi="['jxgl:bjgl:edit']">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['jxgl:bjgl:remove']">删除</el-button>
          <el-button type="warning" icon="el-icon-download" @click="handleExport" v-hasPermi="['jxgl:bjgl:export']">导出</el-button>
        </el-button-group>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </div>

      <!-- 表格区域 -->
      <el-table v-loading="loading" :data="bjglList" @selection-change="handleSelectionChange" border stripe highlight-current-row style="width: 100%">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" label="序号" width="80" align="center" />

        <el-table-column label="班级名称" align="center" prop="bjMc" />
        <el-table-column label="教师姓名" align="center">
          <template slot-scope="scope">
            {{ teacherMap[scope.row.jsId] || '' }}
          </template>
        </el-table-column>
        <el-table-column label="班级人数" align="center" prop="bjRs" />
        <el-table-column label="操作" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['jxgl:bjgl:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['jxgl:bjgl:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <!-- 添加或修改班级管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="班级名称" prop="bjMc">
              <el-input v-model="form.bjMc" placeholder="请输入班级名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="教师姓名" prop="jsXm">
              <el-input v-model="form.jsXm" placeholder="请选择教师" readonly @focus="showJsDialog = true" @click="showJsDialog = true" />
            </el-form-item>
            <el-input v-model="form.jsId" type="hidden" />
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="班级人数" prop="bjRs">
              <el-input v-model="form.bjRs" placeholder="请输入班级人数" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      <js-xx-dx :visible.sync="showJsDialog" @select="handleJsSelect" />
    </el-dialog>
  </div>
</template>

<script>
import { listBjgl, getBjgl, delBjgl, addBjgl, updateBjgl } from "@/api/jxgl/bjgl"
import JsXxDx from '@/views/components/xsxxxz/JsXxDx.vue'

export default {
  name: "Bjgl",
  components: { JsXxDx },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班级管理表格数据
      bjglList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bjMc: null,
        jsId: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bjMc: [
          { required: true, message: "班级名称不能为空", trigger: "blur" }
        ],
        jsId: [
          { required: true, message: "教师ID不能为空", trigger: "blur" }
        ],
        bjRs: [
          { required: true, message: "班级人数不能为空", trigger: "blur" }
        ],
      },
      showJsDialog: false,
      teacherMap: {},
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询班级管理列表 */
    getList() {
      this.loading = true
      listBjgl(this.queryParams).then(response => {
        this.bjglList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        bjglId: null,
        bjMc: null,
        jsId: null,
        bjRs: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bjglId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加班级管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const bjglId = row.bjglId || this.ids
      getBjgl(bjglId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改班级管理"
        // 自动赋值教师姓名
        if (this.form.jsId && !this.form.jsXm) {
          this.form.jsXm = this.teacherMap[this.form.jsId] || ''
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bjglId != null) {
            updateBjgl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addBjgl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bjglIds = row.bjglId || this.ids
      this.$modal.confirm('是否确认删除班级管理编号为"' + bjglIds + '"的数据项？').then(function() {
        return delBjgl(bjglIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jxgl/bjgl/export', {
        ...this.queryParams
      }, `bjgl_${new Date().getTime()}.xlsx`)
    },
    handleJsSelect(teacher) {
      this.form.jsId = teacher.jsglId;
      this.form.jsXm = teacher.xm;
      this.teacherMap[teacher.jsglId] = teacher.xm;
    },
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 10px;
  .box-card {
    margin-bottom: 10px !important;
    .card-title {
      font-size: 14px;
      font-weight: bold;
      color: #303133;
    }
  }
  .table-operations {
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .el-button-group {
    .el-button {
      margin-right: 0;
    }
  }
  .el-table {
    margin: 8px 0;
  }
  .el-form {
    .el-form-item {
      margin-bottom: 12px;
    }
  }
  .el-dialog {
    .el-dialog__body {
      padding: 16px 20px;
    }
  }
  .el-form-item__label {
    font-size: 12px !important;
  }
}
</style>
